﻿using LpbPrj.Client.Data;
using LpbPrj.Client.Event;
using LpbPrj.Client.Tools.Helper;
using LpbPrj.Client.Views;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Diagnostics;
using System.Linq;
using System.Speech.Synthesis;
using System.Windows;

namespace LpbPrj.Client.ViewModels
{
    public class MainWindowViewModel : GlobalBaseViewModel
    {
        protected override void MainLoadedEvent(RoutedEventArgs eventArgs)
        {
            mainWindow = eventArgs.Source as MainWindow;
        }

        private MainWindow mainWindow = null;

        public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogService)
            : base(eventAggregator, regionManager, dialogService)
        {
            //语音播报
            eventAggregator.GetEvent<MessageSentEvent>()
                .Subscribe(obj =>
                {
                    if (obj.MsgParam != null)
                    {
                        string textToSpeak = obj.MsgParam.GetValue<string>("Param");
                        if (!string.IsNullOrEmpty(textToSpeak))
                        {
                            speech.SpeakAsyncCancelAll();
                            speech.SpeakAsync(textToSpeak);
                        }
                    }
                }, ThreadOption.UIThread, false, (filter) => filter.MsgKey.Equals(EventKey.Speech));

            //主页面关闭
            eventAggregator.GetEvent<MessageSentEvent>()
                .Subscribe(obj =>
                {
                    if (obj.MsgParam != null && mainWindow != null)
                    {
                        DoActionWithWaiting(new Action(() =>
                        {
                            GlobalData.Save(true);


                        }), waitingTitle: "设备关闭中......", nextUiAction: () =>
                        {
                            if (TouchExtend.HasTouchInput())
                            {
                                int hwnd = Win32Helper.FindWindow("Shell_TrayWnd", "");
                                Win32Helper.ShowWindow(hwnd, Win32Helper.SW_SHOW);
                            }
                            mainWindow.Close();
                            Debug.WriteLine($"MainWindowClose---MainWindowViewModel");
                        });
                    }
                }, ThreadOption.UIThread, false, (filter) => filter.MsgKey.Equals(EventKey.MainWindowClose));

            //子页面加载
            eventAggregator.GetEvent<MessageSentEvent>()
                .Subscribe(obj =>
                {
                    if (obj.MsgParam != null)
                    {
                        string viewName = obj.MsgParam.GetValue<string>("Param");
                        if (!string.IsNullOrEmpty(viewName)) return;
                        Debug.WriteLine($"LoadShowContent---{viewName}");

                        if (SubContent is IDisposable disposable)
                        {
                            disposable.Dispose();
                        }
                        SubContent = AssemblyHelper.CreateInternalInstance($"Views.{viewName}");
                    }
                }, ThreadOption.UIThread, false, (filter) => filter.MsgKey.Equals(EventKey.LoadShowContent));

            //初始化语音
            SpeechInit();
        }


        #region Speech

        /// <summary>
        /// 语音播报对象
        /// </summary>
        private SpeechSynthesizer speech = null;

        /// <summary>
        /// Speech初始化
        /// </summary>
        private void SpeechInit()
        {
            speech = new SpeechSynthesizer();
            speech.Volume = 100; //音量
            speech.Rate = 0;
            //speech.SelectVoice("Microsoft Zira Desktop");//设置播音员 英文
            //speech.SelectVoice("Microsoft Huihui Desktop");//设置播音员 中文
            var voices = speech.GetInstalledVoices().ToList();
            if (voices.Select(p => p.VoiceInfo.Name).Contains("Microsoft Huihui Desktop"))
            {
                speech.SelectVoice("Microsoft Huihui Desktop");//设置播音员 中文
            }
        }

        /// <summary>
        /// Speech释放
        /// </summary>
        private void SpeechUnInit()
        {
            if (speech != null)
            {
                speech.Pause();
                speech.Dispose();
            }
        }

        /// <summary>
        /// 语音是否打开
        /// </summary>
        private bool _speechOpened = true;
        public bool SpeechOpened { get => _speechOpened; set => SetProperty(ref _speechOpened, value); }

        #endregion

        /// <summary>
        /// 字内容
        /// </summary>
        private object _subContent = null;
        public object SubContent { get => _subContent; set => SetProperty(ref _subContent, value); }

    }

}
